//
//  FKWithdrawVC.m
//  WanTao
//
//  Created by Macbook Pro on 2019/3/29.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKWithdrawVC.h"
#import "FKChooseBankcardVC.h"
#import "FKPayPwdInputView.h"

@interface FKWithdrawVC ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIView *header;
@property (weak, nonatomic) IBOutlet UIButton *wechatBtn;
@property (weak, nonatomic) IBOutlet UIButton *bankcardBtn;
@property (weak, nonatomic) IBOutlet UILabel *bankLabel;
@property (weak, nonatomic) IBOutlet UITextField *cardField;
@property (weak, nonatomic) IBOutlet UITextField *amountField;
@property (weak, nonatomic) IBOutlet UILabel *balanceLabel;
@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;

@property (nonatomic, strong) NSDictionary *selectedBank;
@property (nonatomic, strong) NSMutableDictionary *param;

@end

@implementation FKWithdrawVC

- (NSMutableDictionary *)param
{
    if (!_param) {
        _param = [NSMutableDictionary dictionary];
        _param[@"type"] = @"1"; // 默认使用微信提现
    }
    return _param;
}

- (instancetype)init
{
    return [UIStoryboard storyboardWithName:@"FKWithdrawVC" bundle:nil].instantiateInitialViewController;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (self.balance) {
        self.balanceLabel.text = fkFORMAT(@"可提现余额: %@元", self.balance);
    }
}

- (IBAction)wechatBtnClicked:(UIButton *)sender
{
    if (sender.isSelected) return;
    
    sender.selected = true;
    self.bankcardBtn.selected = false;
    self.param[@"type"] = @"1";
    
    self.header.fk_height = 65;
    self.tableView.tableHeaderView = self.header;
}

- (IBAction)bankcardBtnClicked:(UIButton *)sender
{
    if (sender.isSelected) return;
    
    sender.selected = true;
    self.wechatBtn.selected = false;
    self.param[@"type"] = @"2";
    
    self.header.fk_height = 120;
    self.tableView.tableHeaderView = self.header;
}

- (IBAction)bankViewDidTap:(UITapGestureRecognizer *)sender
{
    FKChooseBankcardVC *vc = [[FKChooseBankcardVC alloc] init];
    if (self.selectedBank) {
        vc.bankCard = self.selectedBank;
    }
    fkPushNav(vc);
    fkWeakSelf(self);
    vc.selectedBankCard = ^(NSDictionary * _Nonnull bankcard) {
        weakself.selectedBank = bankcard;
        weakself.bankLabel.text = bankcard[@"name"];
        weakself.cardField.text = bankcard[@"bank_number"];
    };
}

- (IBAction)allWithdrawBtnClicked:(UIButton *)sender
{
    if (self.balance) {
        self.amountField.text = self.balance;
    }
}

- (IBAction)confirmBtnClicked:(UIButton *)sender
{
    if ([self.param[@"type"] isEqualToString:@"2"]) {
        if (!self.selectedBank) {
            [SVProgressHUD showInfoWithStatus:@"请选择银行卡"];
            return;
        }
    }
    
    FKPayPwdInputView *view = [[FKPayPwdInputView alloc] init];
    [view show];
    fkWeakSelf(self);
    view.inputFinishBlock = ^(NSString *pwd) {
        [weakself withdraw:pwd];
    };
    
}

- (IBAction)amountFieldValueChanged:(UITextField *)sender
{
    self.confirmBtn.enabled = sender.text.length > 0;
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *finalText = [textField.text stringByAppendingString:string];
    if ([finalText fk_isRightPrice]) {
        return true;
    }
    return false;
}

- (void)withdraw:(NSString *)pwd
{
    self.param[@"pwd"] = pwd;
    self.param[@"money"] = self.amountField.text;
    self.param[@"bank_card_id"] = self.selectedBank[@"id"];
    [FKHttpTool postWithSuffix:@"m=Home&c=Withdraw&a=withdraw" param:self.param success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            [SVProgressHUD showSuccessWithStatus:@"提现申请成功，请耐心等待审核"];
//            [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshBalance" object:nil];
            fkPopVC;
        }
    } failure:nil];
}

@end
